package com.xuyisen.config;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/*
 * 全局异常处理
 */

@ControllerAdvice
public class GlobalDefaultExceptionHandler {

	HashMap<String, Object> hashMap = new HashMap<>();

	// 异常处理机制(404)
	@ExceptionHandler(IllegalArgumentException.class)
	@ResponseBody
	public HashMap<String, Object> handleBadRequests() throws IOException {
		hashMap.put("status", "404");
		return hashMap;
	}

	// 异常处理机制(500)
	@ResponseBody
	@ExceptionHandler(NullPointerException.class)
	public HashMap<String, Object> handleInteralError(HttpServletResponse response) throws IOException {
		hashMap.put("status", "500");
		return hashMap;
	}

}
